/*
 * Copyright (C) 2011-2020 ShenZhen iBOXCHAIN Information Technology Co.,Ltd.
 * All right reserved.
 * This software is the confidential and proprietary
 * information of iBOXCHAIN Company of China.
 * ("Confidential Information"). You shall not disclose
 * such Confidential Information and shall use it only
 *  in accordance with the terms of the contract agreement
 *  you entered into with iBOXCHAIN inc.
 *
 */

package com.demo.simpletestapp.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.view.Display;

import com.demo.simpletestapp.SimpleApp;

import java.lang.reflect.Method;

public class DisplayUtil {

    /**
     * 获取手机出厂时默认的densityDpi
     *
     * @return
     */
    @SuppressLint("PrivateApi")
    public static int getDefaultDisplayDensity() {

        try {
            Class<?> aClass = Class.forName("android.view.WindowManagerGlobal");
            Method method = aClass.getMethod("getWindowManagerService");
            method.setAccessible(true);
            Object iwm = method.invoke(aClass);
            Method getInitialDisplayDensity = iwm.getClass().getMethod("getInitialDisplayDensity", int.class);
            getInitialDisplayDensity.setAccessible(true);
            Object densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY);
            return (int) densityDpi;
        } catch (Exception e) {
            e.printStackTrace();
            return 240;
        }
    }

    /**
     * 获取手机出厂时默认的densityDpi并设置给手机
     * 在需要用到的activity中的onCreate中调用该方法
     *
     * @param context
     */
    public static void setDefaultDisplay(Context context) {
        if (Build.VERSION.SDK_INT > 23) {
            Configuration origConfig = context.getResources().getConfiguration();
            origConfig.densityDpi = getDefaultDisplayDensity();//获取手机出厂时默认的densityDpi
            context.getResources().updateConfiguration(origConfig, context.getResources().getDisplayMetrics());
        }
    }


    public static float getDPIFactorOfDevices() {
        if (isG2()) {
            return 375f / 240f;
        } else {
            return 1f;
        }

    }

    /**
     * 02和05大部分功能都一样，所以同时判断
     * @return
     */
    public static boolean isG2() {
        return DisplayUtil.get(SimpleApp.Companion.getInstance(), "ro.product.model2").equals("SNL02") ||
                DisplayUtil.get(SimpleApp.Companion.getInstance(), "ro.product.model2").equals("SNL05");
    }

    /**
     * 是不是5s版本
     * @return
     */
    public static boolean is5s() {
        return DisplayUtil.get(SimpleApp.Companion.getInstance(), "ro.product.model2").equals("SNL05");
    }

    public static boolean is5Pro(){
        if(isG2()){
            return DisplayUtil.get(SimpleApp.Companion.getInstance(), "ro.product.cdyname").contains("5Pro");
        }else{
            return false;
        }
    }

    public static String get(Context context, String key) throws IllegalArgumentException {
        String ret = "";
        try {
            ClassLoader cl = context.getClassLoader();
            @SuppressWarnings("rawtypes")
            Class SystemProperties = cl.loadClass("android.os.SystemProperties");
            //参数类型
            @SuppressWarnings("rawtypes")
            Class[] paramTypes = new Class[1];
            paramTypes[0] = String.class;
            Method get = SystemProperties.getMethod("get", paramTypes);
            //参数
            Object[] params = new Object[1];
            params[0] = new String(key);
            ret = (String) get.invoke(SystemProperties, params);
        } catch (IllegalArgumentException iAE) {
            throw iAE;
        } catch (Exception e) {
            ret = "";
        }
        return ret;
    }
}
